package cn.itcast.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class CalRunTotalTimeAspect {
    @Around("execution(* cn.itcast.service.*.*(..))")
    public Object calTime(ProceedingJoinPoint pjp) throws Throwable{
        long start = System.currentTimeMillis();
        try {
            Object proceed = pjp.proceed();
            return proceed;
        } finally {
            long end = System.currentTimeMillis();
            // 执行方法
            Signature signature = pjp.getSignature();
            // 获取方法名
            String name = signature.getName();
            //获取全类名
            String clazzName = signature.getDeclaringTypeName();
            log.info("{}类中的{}方法执行耗时：{}ms", clazzName, name, (end - start));

        }

    }
}
